//
//  ReadListController.m
//  liang ping
//
//  Created by lanou on 16/3/11.
//  Copyright © 2016年 孙金龙. All rights reserved.
//

#import "ReadListController.h"
#import "ReadListCell.h"
#import "ReadDetailController.h"
#import "RequestManager.h"
#import "ProductModel.h"
@interface ReadListController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UIScrollView *scroll;
@property(nonatomic,strong)UIButton *newlabel;
@property(nonatomic,strong)UIButton *hotLabel;
@property(nonatomic,strong)NSMutableArray *modeArray1;
@property(nonatomic,strong)NSMutableArray *modelArray2;
@property(nonatomic,assign)BOOL new;
@end

@implementation ReadListController

// 网络请求
- (void)NetWorkingAddtime
{
    NSArray *keyArray = @[@"deviceid",@"typeid",@"client",@"sort",@"limit",@"version",@"auth",@"start"];
    NSArray *valueArray = @[@"63A94D37-33F9-40FF-9EBB-481182338873",[NSNumber numberWithInteger:_type],@"1",@"addtime",@"limit",@"3.0.2",@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQ x1c4P0",@"0"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    [RequestManager requestWithUrl:@"http://api2.pianke.me/read/columns_detail" requestType:RequestTypePost parDic:dic finish:^(NSData *data) {
        NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic =Dic[@"data"];
        NSArray *listArray = dataDic[@"list"];
        // NSLog(@"%@",data);
        self.modeArray1 = [NSMutableArray array];
        for (NSDictionary *modelDic in listArray)
        {
            ProductModel *model = [[ProductModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.modeArray1 addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView1 reloadData];
        });
    } error:^(NSError *error) {
        
    }];
}

- (void)NetWorkingHot
{
    NSArray *keyArray = @[@"deviceid",@"typeid",@"client",@"sort",@"limit",@"version",@"auth",@"start"];
    NSArray *valueArray = @[@"63A94D37-33F9-40FF-9EBB-481182338873",[NSNumber numberWithInteger:_type],@"1",@"hot",@"limit",@"3.0.2",@"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQ x1c4P0",@"0"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
    [RequestManager requestWithUrl:@"http://api2.pianke.me/read/columns_detail" requestType:RequestTypePost parDic:dic finish:^(NSData *data) {
        NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = Dic[@"data"];
        NSArray *listArray = dataDic[@"list"];
        self.modelArray2 = [NSMutableArray array];
        for (NSDictionary *modelDic in listArray)
        {
            ProductModel *model = [[ProductModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.modelArray2 addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView2 reloadData];
        });
    } error:^(NSError *error) {
        
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self NetWorkingAddtime];
    [self NetWorkingHot];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 20, KWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 72, KWidth, KHeight-72)];
    self.scroll.contentSize = CGSizeMake(KWidth*2, KHeight-72);
    self.scroll.contentOffset = CGPointMake(0, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.showsVerticalScrollIndicator = NO;
    self.scroll.delegate = self;
    [self.view addSubview:_scroll];
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(5, 5, 40, 40);
    [self.button setImage:[UIImage imageNamed:@"u9_start.png" ]forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_button];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 1, 50)];
    label1.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 71, KWidth, 1)];
    label2.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:label2];
    
    UILabel *xxx = [[UILabel alloc] initWithFrame:CGRectMake(60, 5, 200, 40)];
    xxx.text = _Title;
    xxx.font = [UIFont systemFontOfSize:17];
    xxx.textColor = [UIColor blackColor];
    [view addSubview:xxx];
    
    _newlabel = [UIButton buttonWithType:UIButtonTypeSystem];
    _newlabel.frame = CGRectMake(KWidth-120, 15, 35, 30);
    _newlabel.backgroundColor = [UIColor grayColor];
    [_newlabel setTitle:@"New" forState:UIControlStateNormal];
    [_newlabel setTintColor:[UIColor whiteColor]];
    [_newlabel addTarget:self action:@selector(newlabelAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_newlabel];
    
    _hotLabel = [UIButton buttonWithType:UIButtonTypeSystem];
    _hotLabel.backgroundColor  = [UIColor lightGrayColor];
    _hotLabel.frame = CGRectMake(KWidth-60, 15, 30, 30);
    [_hotLabel setTintColor:[UIColor whiteColor]];
    [_hotLabel setTitle:@"Hot" forState:UIControlStateNormal];
    [_hotLabel addTarget:self action:@selector(hotLabelAction) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_hotLabel];
    
    self.tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-72)style:UITableViewStylePlain];
    self.tableView1.dataSource = self;
    self.tableView1.delegate = self;
    self.tableView1.tag = 100;
    [self.tableView1 registerClass:[ReadListCell class] forCellReuseIdentifier:@"listcell"];
    
    self.tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(KWidth, 0, KWidth, KHeight-72)style:UITableViewStylePlain];
    self.tableView2.dataSource = self;
    self.tableView2.delegate = self;
    self.tableView2.tag = 200;
    [self.tableView2 registerClass:[ReadListCell class] forCellReuseIdentifier:@"listcell"];
    
    [self.scroll addSubview:_tableView1];
    [self.scroll addSubview:_tableView2];
}

// 结束减速的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    switch ((NSInteger)(scrollView.contentOffset.x/KWidth))
    {
        case 0:
            _newlabel.backgroundColor = [UIColor grayColor];
            _hotLabel.backgroundColor = [UIColor lightGrayColor];
            break;
        case 1:
            _newlabel.backgroundColor = [UIColor lightGrayColor];
            _hotLabel.backgroundColor = [UIColor grayColor];
        default:
            break;
    }
}


// newButton点击事件
- (void)newlabelAction
{
    [UIView animateWithDuration:1 animations:^{
        self.scroll.contentOffset = CGPointMake(0, 0);
    }];
    _newlabel.backgroundColor = [UIColor grayColor];
    _hotLabel.backgroundColor = [UIColor lightGrayColor];
}

// hotButton点击事件
- (void)hotLabelAction
{
    [UIView animateWithDuration:1 animations:^{
        self.scroll.contentOffset = CGPointMake(KWidth, 0);
    }];
    _newlabel.backgroundColor = [UIColor lightGrayColor];
    _hotLabel.backgroundColor = [UIColor grayColor];
}


// 返回button
- (void)buttonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 100)
    {
        return self.modeArray1.count;
    }
    else
    {
        return self.modelArray2.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listcell" forIndexPath:indexPath];
    if (tableView.tag == 100)
    {
        cell.model = self.modeArray1[indexPath.row];
    }
    else
    {
        cell.model = self.modelArray2[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 205;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadDetailController *detail = [[ReadDetailController alloc] init];
    if (tableView.tag == 100)
    {
        ProductModel *model = self.modeArray1[indexPath.row];
        detail.contentId = model.Id;
    }
    else
    {
        ProductModel *model = self.modelArray2[indexPath.row];
        detail.contentId = model.Id;
    }
    detail.Title = _Title;
    NSLog(@"===========%@",_Title);
    [self presentViewController:detail animated:YES completion:nil];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
